var app = getApp()
Page({
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1200,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,

    windowWidth: 375,
    windowHeight: 571,
    otherheight: 20,
    contentheight: 500,
    arraydetail: [],
    arraypin: [],
    pageIndex: 1,
    pagePinIndex: 1,
    scrollTop: 0,
    scrollHeight: 0,
    spcateId: 0,
    loadding: true,
    author: 'Lotus'
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.shopName,
      path: '/pages/index/pinlist?fromInviteCode=' + app.globalData.myInviteCode ,  
      success: function (res) {
        //console.log("转发成功");
      }
    }
  },  
  addnow: function (e) {
    var productId = e.target.dataset.id;
    wx.redirectTo({
      url: 'productdetail?productId=' + productId
    })
  },
  joinnow: function (e) {
    var productId = e.target.dataset.id;
    var pinId = e.target.dataset.pinid;
    wx.redirectTo({
      url: 'pindetail?productId=' + productId + '&pinId=' + pinId
    })
  },  
  getproduct: function () {
    var that = this;
    that.setData({
      loadding: false
    });
    var service = 'ProductPin.getProductsPinList';
    var top = 0;
    var spcateId = that.data.spcateId;
    var pageIndex = that.data.pageIndex;
    var arraydetail = that.data.arraydetail;
    var pageSize = 5;
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, appId: app.globalData.APPID, top: top, spcateId: spcateId, pageIndex: pageIndex, pageSize: pageSize },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        //console.log("success:" + res.data.data);
        if (!res.data.data.code) {
          var dataresult = res.data.data.list.list;
          for (var i in dataresult) {
            arraydetail.push(dataresult[i]);
          }
          pageIndex++;
          that.setData({
            arraydetail: arraydetail,
            pageIndex: pageIndex,
            loadding: true
          })
        }
      },
    })
  },
  getpinlist: function () {
    var that = this;
    var service = 'ProductPin.getPinsList';
    var pinStatus = 1;
    var spcateId = that.data.spcateId;
    var pagePinIndex = that.data.pagePinIndex;
    var pageSize = 5;
    var arraypin = that.data.arraypin;
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, appId: app.globalData.APPID, pinStatus: pinStatus, spcateId: spcateId, pageIndex: pagePinIndex, pageSize: pageSize },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (!res.data.data.code) {
          var dataresult = res.data.data.list.list;
          for (var i in dataresult) {
            if (arraypin[dataresult[i].productId]) {
              dataresult[i].mypin = 1;
            } else {
              dataresult[i].mypin = 0;
            }
            arraypin.push(dataresult[i]);
          }
          pagePinIndex++;
          that.setData({
            arraypin: arraypin,
            pagePinIndex: pagePinIndex,
            loadding: true
          })
        }

      },
    })
  },
  scroll: function (event) {
    //   该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  getmoredata: function () {
    //   该方法绑定了页面滑动到底部的事件
    var currentTab = this.data.currentTab;
    if (currentTab == 0) {
      this.getproduct();
    } else {
      this.getpinlist();
    }

  },
  bindChange: function (e) {//滑动切换tab
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  swichNav: function (e) {//点击tab切换 
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      if (e.target.dataset.current == 0) {
        that.getproduct();
      } else {
        that.getpinlist();
      }

    }
  },
  onLoad: function (options) {
    var that = this;
    if (options.fromInviteCode) {
      app.globalData.fromInviteCode=options.fromInviteCode;
    }
    app.appstart();
   if (options.cateId) {
      that.setData({ spcateId: options.cateId });
    }

    //这里要非常注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
    wx.getSystemInfo({
      success: function (res) {
        //console.info(res.windowHeight);
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          scrollHeight: res.windowHeight
        });
      }
    });
    
    var currentTab = that.data.currentTab;
    if (currentTab == 0) {
      that.getproduct();
    } else {
      that.getpinlist();
    }  
  }
})